﻿
using System;
using System.Data;
using DevExpress.XtraEditors;

namespace Mota.XMLControls
{
    /// <summary>
    /// Represent MaskText based on DX TextEdit
    /// </summary>
    public class DXGuiMaskText:GuiMaskText
    {
        public DXGuiMaskText()
        {
            visualControl = new TextEdit();
            (visualControl as TextEdit).Properties.Mask.MaskType = DevExpress.XtraEditors.Mask.MaskType.RegEx;
            (visualControl as TextEdit).EditValueChanged += DXGuiMaskText_EditValueChanged;
        }

        void DXGuiMaskText_EditValueChanged(object sender, EventArgs e)
        {
            this.ChangeEventCall(null);
        }
        /// <summary>
        /// SET|GET TextEdit.Text property 
        /// </summary>
        public override string Text
        {
            get
            {
                return (visualControl as TextEdit).Text;
            }
            set
            {
                (visualControl as TextEdit).Text = value;
            }
        }
        /// <summary>
        /// SET|GET TextEdit.Mask property
        /// </summary>
        public override String Mask
        {
            get
            {
                return (visualControl as TextEdit).Properties.Mask.EditMask;
            }
            set
            {
                (visualControl as TextEdit).Properties.Mask.EditMask = value;
            }
        }

        public override bool IsValid
        {
            get
            {
                return Required ? (!String.IsNullOrEmpty((visualControl as TextEdit).EditValue as String)) : true;
            }
        }
        /// <summary>
        /// Add databinding to TextEdit control on Text property
        /// </summary>
        /// <param name="valueBinding">main binding source with current record values</param>
        public override void SetValue(object valueBinding)
        {
            if(valueBinding is System.Windows.Forms.BindingSource)
                (visualControl as TextEdit).DataBindings.Add("Text", valueBinding as System.Windows.Forms.BindingSource,
                                                            XmlAttributes["XMLAttribute"], true);

        }
        /// <summary>
        /// Dummy property and getter allways return null
        /// </summary>
        public override object Binding
        {
            get
            {
                return null;
            }
            set
            {
             
            }
        }

        public override object GetValue()
        {
            return (visualControl as TextEdit).Text;
        }
    }
}
